<template>
  <div style="margin-left:520px;margin-right:520px;">
    <el-main class="post-article">
      <el-form id="#create" :model="create" :rules="rules" ref="create" label-width="40%">
        <el-form-item label="法律法规名称" prop="lawName">
          <el-input v-model="create.lawName"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="create.status" placeholder="请选择">
            <el-option
              v-for="item in optionsStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="颁布时间" prop="issueTime">
          <el-date-picker v-model="create.issueTime" type="date" placeholder="选择颁布时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="录入时间" prop="inputTime">
          <el-date-picker v-model="create.inputTime" type="date" placeholder="选择录入时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="录入人" prop="inputPerson">
          <el-input v-model="create.inputPerson"></el-input>
        </el-form-item>
        <el-form-item label="法律类别" prop="lawType">
          <el-select v-model="create.lawType" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属部门" prop="lawGroup">
          <el-select v-model="create.lawGroup" placeholder="请选择">
            <el-option
              v-for="item in optionsGroup"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="法律条款明细" prop="lawDetail">
          <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="create.lawDetail"></el-input>
        </el-form-item>
        <el-button @click="back()">返 回</el-button>
        <el-button type="primary" @click="saveLaws()">保 存</el-button>
      </el-form>
    </el-main>
  </div>
</template>


<script>
var placeholders = {
  name: "请输入查找姓名",
  username: "请输入查找用户名",
  phone: "请输入查找电话"
};
export default {
  data: function() {
    return {
      create: {
        lawName: "",
        status: "",
        issueTime: "",
        inputTime: "",
        inputPerson: "",
        lawType: "",
        lawGroup: "",
        lawDetail: "",
        is_active: true
      },
      rules: {
        lawName: [
          {
            required: true,
            message: "请输入法律法规名称",
            trigger: "blur"
          },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符"
          }
        ],
        inputPerson: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          },
          {
            min: 3,
            max: 25,
            message: "长度在 3 到 25 个字符"
          }
        ],
        lawType: [
          {
            required: true,
            message: "请选择类别",
            trigger: "blur"
          }
        ],
        lawGroup: [
          {
            required: true,
            message: "请选择部门",
            trigger: "blur"
          }
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "blur"
          }
        ],
        checkPass: [
          {
            required: true,
            message: "请再次输入密码",
            trigger: "blur"
          }
        ],
        email: [
          {
            type: "email",
            message: "邮箱格式不正确"
          }
        ],
        lawDetail: [
          {
            required: true,
            message: "请输入详情",
            trigger: "blur"
          },
          {
            min: 3,
            max: 998,
            message: "长度在 3 到 998 个字符"
          }
        ],
        phone: [
          {
            pattern: /^1[34578]\d{9}$/,
            message: "请输入中国国内的手机号码"
          }
        ]
      },
      options: [
        {
          value: "1",
          label: "全文"
        },
        {
          value: "2",
          label: "法律条文"
        }
      ],
      optionsGroup: [
        {
          value: "01",
          label: "浏阳市民政局"
        },
        {
          value: "02",
          label: "长沙市民政局"
        }
      ],
      optionsStatus: [
        {
          value: "1",
          label: "有效"
        },
        {
          value: "0",
          label: "无效"
        }
      ],
      total_rows: 0,
      loading: true,
      keywords: "", //搜索框的关键字内容
      select: "username", //搜索框的搜索字段
      selected: [], //已选择项
      dialogCreateVisible: false, //创建对话框的显示状态
      dialogUpdateVisible: false, //编辑对话框的显示状态
      createLoading: false,
      updateLoading: false,
      placeholder: placeholders["username"]
    };
  },
  created() {
    // 页面加载之前执行的方法
    this.initData();
  },
  methods: {
    // 保存方法
    saveLaws() {
      var params = new URLSearchParams();
      params.append("lawName", this.create.lawName);
      params.append("lawType", this.create.lawType);
      params.append("status", this.create.status);
      params.append("inputPerson", this.create.inputPerson);
      params.append("lawGroup", this.create.lawGroup);
      params.append("lawDetail", this.create.lawDetail);
      params.append("lawType", this.create.lawType);
      params.append("issueTime", this.create.issueTime);
      params.append("inputTime", this.create.inputTime);
      params.append("lawsNo", this.$route.params.lawsId);
      this.$axios
        .post("/save", params)
        .then(successResponse => {
          this.responseResult = JSON.stringify(successResponse.data);
          console.log("code--->" + successResponse.data.code);
          if (successResponse.data.code == "200") {
            this.$router.replace({ path: "/index" });
          }
        })
        .catch(failResponse => {
          console.log("提交错误" + failResponse.data);
        });
    },
    // 数据初始化  编辑时
    initData() {
      // axios 参数传递
      console.log("edit --->lawsNo:" + this.$route.params.lawsId);
      var objNo = this.$route.params.lawsId;
      if (objNo == "undefined" || objNo == null || objNo == "") {
        return;
      }
      var params = new URLSearchParams();
      // 获取路由传递的参数
      params.append("lawsNo", this.$route.params.lawsId);
      this.$axios
        .post("/look", params)
        .then(successResponse => {
          console.log("data--->" + successResponse.data.code);
          if (successResponse.data.code == "200") {
            this.create.lawName = successResponse.data.lawName;
            this.create.status = successResponse.data.status;
            this.create.issueTime = successResponse.data.issueTime;
            this.create.inputTime = successResponse.data.inputTime;
            this.create.inputPerson = successResponse.data.inputPerson;
            this.create.lawType = successResponse.data.lawType;
            this.create.lawGroup = successResponse.data.lawGroup;
            this.create.lawDetail = successResponse.data.lawDetail;
          }
        })
        .catch(err => {
          console.error(err);
        });
    },
    back() {
      this.$router.replace({ path: "/index" });
    }
  }
};
</script>







